﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.UI.MobileControls;
using RanchBuddy.Core.Providers.Impl;
using RanchBuddy.Core.Services;
using RanchBuddy.Core.Services.Impl;
using StructureMap;

namespace RanchBuddy.Web.Services
{
    public class Navigation
    {
        public static List<MvcSitemapProvider.MvcSiteMapNode> GetAllNodes()
        {
            List<MvcSitemapProvider.MvcSiteMapNode> result = new List<MvcSitemapProvider.MvcSiteMapNode>();
            result.Add(SiteMap.RootNode.ToMvcNode());
            for (int i = 0; i < SiteMap.RootNode.GetAllNodes().Count;i++)
            {
                result.Add((SiteMap.RootNode.GetAllNodes()[i]).ToMvcNode());
            }
            return result;
        }

        public static List<MvcSitemapProvider.MvcSiteMapNode> GetTopNavNodes()
        {
            List<MvcSitemapProvider.MvcSiteMapNode> result = new List<MvcSitemapProvider.MvcSiteMapNode>();
            foreach (MvcSitemapProvider.MvcSiteMapNode node in GetAllNodes())
            {
                if (IsNodeVisible(node) && node.IsTopNav)
                    result.Add(node);
            }
            return result;
        }

        public static List<MvcSitemapProvider.MvcSiteMapNode> GetPrimaryNavNodes()
        {
            List<MvcSitemapProvider.MvcSiteMapNode> result = new List<MvcSitemapProvider.MvcSiteMapNode>();
            foreach (MvcSitemapProvider.MvcSiteMapNode node in GetAllNodes())
            {
                if (IsNodeVisible(node) && node.IsPrimaryNav)
                    result.Add(node);
            }
            return result;
        }

        public static bool IsNodeVisible(MvcSitemapProvider.MvcSiteMapNode Node)
        {
            if(Node != null && Node.IsVisible)
            {
                //login, logout, and register rules
                if (Node.Title.ToLower() == "log out" || 
                    Node.Title.ToLower() == "log in" || 
                    Node.Title.ToLower() == "register")
                {
                    //log out link and user is logged in
                    if (Node.Title.ToLower() == "log out" &&
                        ObjectFactory.GetInstance<IWebContext>().Account != null)
                    {
                        return true;
                    }
                    //log in link and user is not logged in
                    if (Node.Title.ToLower() == "log in" &&
                        ObjectFactory.GetInstance<IWebContext>().Account == null)
                    {
                        return true;
                    }
                    //register link and user is not logged in
                    if (Node.Title.ToLower() == "register" &&
                        ObjectFactory.GetInstance<IWebContext>().Account == null)
                    {
                        return true;
                    }
                }
                else
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsNodeTopNav(MvcSitemapProvider.MvcSiteMapNode Node)
        {
            return Node.IsTopNav;
        }

        public static bool IsNodeAccessible(MvcSitemapProvider.MvcSiteMapNode Node)
        {
            return true;
        }

        public static bool IsTopNavNodeActive(MvcSitemapProvider.MvcSiteMapNode Node)
        {
            MvcSitemapProvider.MvcSiteMapNode currentNode = SiteMap.CurrentNode.ToMvcNode();
            MvcSitemapProvider.MvcSiteMapNode parentNode = null;

            //is the active node the node being compared?
            if (Node == currentNode)
                return true;

            //does the current node have a parent?
            if (currentNode != null && currentNode.ParentNode != null)
            {
                parentNode = currentNode.ParentNode.ToMvcNode();
                //is that parent our Node?
                if (parentNode == Node)
                    return true;
            }

            //still haven't found it?

            //look 10 levels up the tree
            for(int i = 0; i<10; i++)
            {
                if (parentNode != null && parentNode.ParentNode != null)
                {
                    parentNode = currentNode.ParentNode.ToMvcNode();
                    if (parentNode != null && parentNode == Node)
                        return true;
                }
            }

            //made it this far? didn't find it I guess!
            return false;
        }

        public static List<MvcSitemapProvider.MvcSiteMapNode> GetSecondaryNavNodesForCurrentNode()
        {
            List<MvcSitemapProvider.MvcSiteMapNode> result = new List<MvcSitemapProvider.MvcSiteMapNode>();
            MvcSitemapProvider.MvcSiteMapNode parentNode;
            MvcSitemapProvider.MvcSiteMapNode CurrentNode = null;
            if (SiteMap.CurrentNode != null)
            {
                CurrentNode = SiteMap.CurrentNode.ToMvcNode();

                //is the current node the root node? aka home page
                if (CurrentNode == SiteMap.RootNode.ToMvcNode())
                {
                    result = GetRootSecondaryNodes();
                    return result;
                }
                //is the current node a topnav node?
                else if (CurrentNode.IsPrimaryNav)
                {
                    parentNode = CurrentNode;

                    foreach (SiteMapNode node in parentNode.ChildNodes)
                    {
                        bool isNodeVisible = IsNodeVisible(node.ToMvcNode());
                        bool isNodeSecondaryNav = IsNodeSecondaryNav(node.ToMvcNode());
                        if (isNodeVisible && isNodeSecondaryNav)
                            result.Add(node.ToMvcNode());
                    }
                }
                //not a topnav node...find the topnav node
                else
                {
                    parentNode = CurrentNode.ParentNode.ToMvcNode();


                    if (parentNode == SiteMap.RootNode.ToMvcNode())
                        return GetRootSecondaryNodes();

                    for (int i = 0; i == 10; i++)
                    {
                        if (parentNode.IsPrimaryNav)
                            break;
                        else
                            parentNode = parentNode.ParentNode.ToMvcNode();
                    }

                    if (parentNode != null)
                    {
                        foreach (SiteMapNode node in parentNode.ChildNodes)
                        {
                            if (IsNodeVisible(node.ToMvcNode()) &&
                                IsNodeSecondaryNav(node.ToMvcNode()))
                                result.Add(node.ToMvcNode());
                        }
                    }
                }
            }
            return result;
        }

        private static bool IsNodeSecondaryNav(MvcSitemapProvider.MvcSiteMapNode Node)
        {
            if (Node.IsSecondaryNav)
                return true;
            return false;
        }

        private static List<MvcSitemapProvider.MvcSiteMapNode> GetRootSecondaryNodes()
        {
            List<MvcSitemapProvider.MvcSiteMapNode> result = new List<MvcSitemapProvider.MvcSiteMapNode>();
            foreach (MvcSitemapProvider.MvcSiteMapNode node in GetAllNodes())
            {
                if (node != null && node.ToMvcNode().IsSecondaryNav && 
                    IsNodeVisible(node.ToMvcNode()))

                    result.Add(node.ToMvcNode());
            }
            return result;
        }

        public static List<MvcSitemapProvider.MvcSiteMapNode> GetFooterNodes()
        {
            List<MvcSitemapProvider.MvcSiteMapNode> result = new List<MvcSitemapProvider.MvcSiteMapNode>();
            foreach (MvcSitemapProvider.MvcSiteMapNode node in GetAllNodes())
            {
                if (node != null && node.ToMvcNode().IsFooterNav && IsNodeVisible(node.ToMvcNode()))
                    result.Add(node.ToMvcNode());
            }
            return result;
        }
    }
}